package com.zf.mall.message.sms;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zf.mall.common.util.CacheConstants;
import com.zf.mall.message.entity.BaseSMS;
import com.zf.mall.message.entity.RespData;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class AliyunSmsSender implements Sender {

	//aliyun.sms.accessKeyId=LTAI4FcrEUKYqk4X3Gmw4tBb
	//aliyun.sms.accessSecret=CKL36PjbhusFF27hO55cstSRlmcwlB
	//aliyun.sms.signName=\u9104\u8230\u7F51\u7EDC
			
	@Value("${aliyun.sms.accessKeyId}")
	private String accessKeyId;

	@Value("${aliyun.sms.accessSecret}")
	private String accessSecret;

	@Value("${aliyun.sms.signName}")
	private String signName = "默认签名";

	
	public RespData send(BaseSMS sms) {
        
		RespData retData = new RespData();
		// String _signName = new String(signName.getBytes(StandardCharsets.ISO_8859_1),
		// StandardCharsets.UTF_8);
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setSysMethod(MethodType.POST);
		request.setSysDomain("dysmsapi.aliyuncs.com");
		request.setSysVersion("2017-05-25");
		request.setSysAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		request.putQueryParameter("PhoneNumbers", sms.getMobile());
		request.putQueryParameter("SignName", signName);
		request.putQueryParameter("TemplateCode", sms.getTemplateCode());
		request.putQueryParameter("TemplateParam", JSONObject.toJSONString(sms.getParamSet()));
		// logger.error("发送短信的参数==============================: {} , {} ,{},{}", mobile,
		// smsTemplateCode, signName,JSONObject.toJSONString(textInnerMap));
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
			if (response == null || StrUtil.isBlank(response.getData())) {
				retData.setSuccess(false);
				retData.setMessage("短信发送异常, 阿里无响应!");
				return retData;
			}

			JSONObject respData = JSONObject.parseObject(response.getData());
			if (StrUtil.equals(respData.getString("Code"), CacheConstants.OK)) {
				retData.setSuccess(true);
				retData.setMessage("短信发送成功!");
				return retData;
			} else {
				retData.setSuccess(false);
				retData.setMessage(respData.getString("Message"));
				return retData;
			}

		} catch (ServerException e) {
			log.error("短信发送阿里服务端出现系统异常, 信息 : {} , {}", e.getMessage(), e);
			retData.setSuccess(false);
			retData.setMessage("短信发送异常, 请联系平台客服");
			return retData;
		} catch (ClientException e) {
			log.error("短信发送出现系统客户端异常, 信息 : {} , {}", e.getMessage(), e);
			retData.setSuccess(false);
			retData.setMessage("短信发送异常, 请联系平台客服");
			return retData;
		}

	}


	

}
